//
//    Copyright (C) 2012  OinkStudio
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.
package game;

public class RuleDownward extends Rule {

	private static Rule ruleSingleton;

	protected RuleDownward(String ruleName, int priority) {
		super(ruleName, priority);
	}

	@Override
	public void check(int row, int column, Game game) {
		Obolo[][] board = game.getBoard();
		if (checkSouth(row, column, game) != null) {
			int[] compare = checkSouth(row, column, game);
			if (compare[0] < compare[1])
				board[row + 1][column].setOwner(board[row][column].getOwner());
		}
		if (checkNorth(row, column, game) != null) {
			int[] compare = checkNorth(row, column, game);
			if (compare[0] < compare[1])
				board[row - 1][column].setOwner(board[row][column].getOwner());
		}
		if (checkWest(row, column, game) != null) {
			int[] compare = checkWest(row, column, game);
			if (compare[0] < compare[1])
				board[row][column - 1].setOwner(board[row][column].getOwner());
		}
		if (checkEast(row, column, game) != null) {
			int[] compare = checkEast(row, column, game);
			if (compare[0] < compare[1])
				board[row][column + 1].setOwner(board[row][column].getOwner());
		}

	}

	public static Rule getInstance() {
		if (ruleSingleton == null)
			ruleSingleton = new RuleDownward("RuleDownload", 8);
		return ruleSingleton;
	}

}
